package anwb;
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;
import android.util.Log;
 
public class Download {
 
        public final String PATH = "/sd_card/image.test/";  //put the downloaded file here
       
 
        public void DownloadFromUrl(String imageURL) {  //this is the downloader method
                try {
                        URL url = new URL("http://twitpic.com/show/thumb/" + imageURL); //you can write here any link
                        File file = new File(imageURL);
                        long startTime = System.currentTimeMillis();
                        Log.d("ImageManager", "download begining");
                        Log.d("ImageManager", "download url:" + url);
                        Log.d("ImageManager", "downloaded file name:" + imageURL);
                        /* Open a connection to that URL. */
                        URLConnection ucon = url.openConnection();
 
                        /*
                         * Define InputStreams to read from the URLConnection.
                         */
                        InputStream is = ucon.getInputStream();
                        BufferedInputStream bis = new BufferedInputStream(is);
 
                        /*
                         * Read bytes to the Buffer until there is nothing more to read(-1).
                         */
                        ByteArrayBuffer baf = new ByteArrayBuffer(50);
                        int current = 0;
                        while ((current = bis.read()) != -1) {
                                baf.append((byte) current);
                        }
 
                        /* Convert the Bytes read to a String. */
                        FileOutputStream fos = new FileOutputStream(PATH + file + ".jpg");
                        fos.write(baf.toByteArray());
                        fos.close();
                        Log.d("ImageManager", "download ready in"
                                        + ((System.currentTimeMillis() - startTime) / 1000)
                                        + " sec");

/*
                        File imageFile = new File("/sd_card/image.test/" + imageURL + ".jpg");
                        
                        if(imageFile.exists()){
                        	Log.d("ImageManager", "File " + imageURL + ".jpg exists on SD card \n");
                        	Bitmap myBitmap = BitmapFactory.decodeFile("/sd_card/image.test/" + imageURL + ".jpg");
                        	ImageView myImage = (ImageView) findViewById(R.id.importfile);
                        	myImage.setImageBitmap(myBitmap);
                        	setContentView(R.layout.importfile);
                        	}
                        */
                } catch (IOException e) {
                        Log.d("ImageManager", "Error: " + e);
                }
 
        }
}